<?php
    namespace app\htapi\controller;
    use app\api\controller\Base;
    use think\Controller as Controller;
    use think\Request;
    use think\Session;

    class Index extends Base
    {
        public function _initialize()
        {
            $data = parent::_initialize();
        }
        public function index(){
            $token = input('post.token');
            $userInfo = db('user')->where(['token' => $token])->find();
            $info = db('loginrecord')->where(['user_id' => $userInfo['id']])->order(['id' => 'desc'])->limit(1,1)->select();
            if(!$userInfo){
                return show(0,'token无效');
            }
            $url='http://ip.taobao.com/service/getIpInfo.php?ip='.$info[0]['last_login_ip'];
            $result = file_get_contents($url);
            $resultArr = json_decode($result,true);
            if($resultArr['data']['county'] == 'XX'){
                $resultArr['data']['county'] = '';
            }
            $address = $resultArr['data']['region'].$resultArr['data']['city'].$resultArr['data']['county'];
            $info[0]['last_login_time'] = date('Y-m-d h:i:s', $info[0]['last_login_time']);
            $data = [
                'last_login_time' => $info[0]['last_login_time'],
                'address' => $address
            ];
            $data = array_merge($data, $this->data);
            return show(1,'success',$data);
        }
        /*
         * 图片上传
         */
        public function upload(){
            $data = input('post.');
            if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $data['imgurl'], $result)){
                $type = $result[2];
                $new_file = './uploads/images/'.date('YmdHis', time()).'.'.$type;
                if (file_put_contents($new_file, base64_decode(str_replace($result[1], '', $data['imgurl'])))){
                    $image = $new_file;
                }
                $data['imgurl'] = 'https://vae.scott1995.top'.$image;
            }
            db('banner')->insert($data);
        }
        /*
         * banner图列表
         */
        public function bannerList(){
           $bannerList = db('banner')->select();
           if(!$bannerList){
               return show(0,'error');
           }
           return show(1,'success',$bannerList);
        }
        public function update(){
            $data = input('post.');
            if(isset($data['listorder'])){
                $res = db('banner')->where(['id' => $data['id']])->update(['listorder' => $data['listorder']]);
            } elseif(isset($data['is_show'])){
                $res = db('banner')->where(['id' => $data['id']])->update(['is_show' => $data['is_show']]);
            } elseif (isset($data['imgurl'])){
                if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $data['imgurl'], $result)){
                    $type = $result[2];
                    $new_file = './uploads/images/'.date('YmdHis', time()).'.'.$type;
                    if (file_put_contents($new_file, base64_decode(str_replace($result[1], '', $data['imgurl'])))){
                        $image = $new_file;
                    }
                    $data['imgurl'] = 'https://vae.scott1995.top'.$image;
                }
                $res = db('banner')->where(['id' => $data['id']])->update(['imgurl' => $data['imgurl']]);
            }
            if(!$res){
                return show(0,'更新失败');
            }
            return show(1,'更新成功');
        }
        public function delete(){
            $data = input('post.');
            if(empty($data)){
                return show(0,'参数不合法');
            }
            $data['id'] = json_decode($data['id'],true);
            $res = db('banner')->delete($data['id']);
            if(!$res){
                return show(0,'删除失败');
            }
            return show(1,'删除成功');
        }
    }